package com.imooc.apigateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * @Author snail
 * @Description: zuul跨域处理
 * @create: 2020-04-28 11:15
 * @Param $
 * @return $
 * @Version 1.0
 **/
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        final UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config=new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedOrigins(Arrays.asList("*")); //设置数据源
        config.setAllowedHeaders(Arrays.asList("*")); //设置请求头
        config.setAllowedMethods(Arrays.asList("*")); //设置GET或者POST 等请求方法
        config.setMaxAge(300l);

        source.registerCorsConfiguration("/**",config);
        return new CorsFilter(source);
    }
}
